﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public enum SceneLoadingPhase {
    Start,
    Stable,
    End,
}

public class Loading : MonoBehaviour {
    [Header("绑定")]
    public _EventManager _EventManager;
    public CanvasGroup window;

    [Header("runtime")]
    private SceneLoadingPhase sceneLoadingPhase = SceneLoadingPhase.Start;
    private float time;

    // Start is called before the first frame update
    void Start() {
        _EventManager.__AttachEvent("LoadingStart", LoadingStart);
        _EventManager.__AttachEvent("LoadingStable", LoadingStable);
        _EventManager.__AttachEvent("LoadingEnd", LoadingEnd);
        _EventManager.__AttachEvent("LoadingFinish", LoadingFinish);
        window.alpha = 0;
        window.gameObject.SetActive(false);
    }

    // Update is called once per frame
    void Update() {
        if (GlobalData.loading == true) {
            switch (sceneLoadingPhase) {
                case SceneLoadingPhase.Start:
                    time += Time.deltaTime;
                    window.alpha = time / GlobalData.loadingStartTime;
                    break;

                case SceneLoadingPhase.End:
                    time += Time.deltaTime;
                    window.alpha = 1 - time / GlobalData.loadingEndTime;
                    break;
            }
        }
    }

    private void LoadingStart(object o) {
        time = 0f;
        sceneLoadingPhase = SceneLoadingPhase.Start;
        window.gameObject.SetActive(true);
    }

    private void LoadingStable(object o) {
        sceneLoadingPhase = SceneLoadingPhase.Stable;
        window.alpha = 1;
    }

    private void LoadingEnd(object o) {
        time = 0f;
        sceneLoadingPhase = SceneLoadingPhase.End;

    }

    private void LoadingFinish(object o) {
        window.gameObject.SetActive(false);
    }

}
